﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPLService.DAL;
using OPLService.BusinessEntities;

namespace OPLService.Facade
{
    public class OPLServiceFacade
    {
        OPLDataAccess _dataAccess;
        /// <summary>
        /// Returns the notifications which are entered into the system after dateOffset and within the interval.
        /// </summary>
        /// <param name="dateOffset"></param>
        /// <param name="interval"></param>
        /// <returns></returns>
        public DTO GetNotifications(ulong dateOffset, double interval)
        {
            _dataAccess = new OPLDataAccess();
            DTO dtoResult;

            try
            {
                DateTime utcDateOffset = new DateTime(1970, 1, 1, 0, 0, 0);
                utcDateOffset = utcDateOffset.AddMilliseconds(dateOffset);
                DateTime startDate = utcDateOffset.AddMilliseconds(-interval);
                
                List<NotificationDetails> lstNotifications = _dataAccess.GetNotifications(startDate);
                dtoResult = new DTO( lstNotifications);
            }
            catch (Exception ex)
            {
                dtoResult = new DTO(GetExceptionDetails( ex),ErrorCode.Failed);
            }

            return dtoResult;
        }
        public static string GetExceptionDetails(Exception ex)
        {
            //TODO Configure it to block the details from sent to client to avoid security issues.
            if (ex != null)
            {
                return string.Format("Message :{0}" + Environment.NewLine + "StackTrace : {1}" + Environment.NewLine + GetExceptionDetails(ex.InnerException)+Environment.NewLine, ex.Message, ex.StackTrace);
            }
            else return string.Empty;
        }

        public DTO AddNotifications(string notifyString)
        {
            _dataAccess = new OPLDataAccess();
            DTO dtoResult;

            try
            {
                Int32 notifID = _dataAccess.AddNotifications(notifyString);
                dtoResult = new DTO(notifID);
            }
            catch (Exception ex)
            {
                dtoResult = new DTO(ex.Message, ErrorCode.Failed);
            }
            return dtoResult;
        }

        /// <summary>
        /// Gets the latest notifications .Number limits by either count or the lower limit which ever meets first.
        /// </summary>
        /// <param name="count"></param>
        /// <param name="lowerLimit"></param>
        /// <returns></returns>
        public DTO GetLatestNotifications(int count, int lowerLimit)
        {
            _dataAccess = new OPLDataAccess();
            DTO dtoResult;

            try
            {
                List<NotificationDetails> lstNotifications = _dataAccess.GetLatestNotifications(count, lowerLimit);
                dtoResult = new DTO(lstNotifications);
            }
            catch (Exception ex)
            {
                dtoResult = new DTO(GetExceptionDetails(ex), ErrorCode.Failed);
            }

            return dtoResult;
        }
    }
}
